<?php
class SearchController extends Zendvn_Controller_Action
{
	public function init()
	{
		$template_path=TEMPLATE_PATH."/public/system";
		$this->loadTemplate($template_path);
		
	}
	
	public function searchAction()  // view helper searchMenu() =='TIM KIEM LAPTOP' tren trinh duyet.
	{
		if($this->_request->isPOST())
		{
			$tmp=$this->_request->getPOST();
			$txtKey=$tmp['txtKey'];
			$category=$tmp['category'];
			$price1=$tmp['price1'];
			$price2=$tmp['price2'];
			if($txtKey==null && $category==0 && $price1==0 && $price2==0)
			{
				echo "<script>alert('Báº¡n Pháº£i chá»�n giÃ¡ trá»‹')</script>";
				$this->view->productCat=null;
			}
			else
			{
				$product= new Default_Model_Product();
				$searchsell= $product->getSearch($txtKey,$category,$price1,$price2);
			}
			$this->view->productCat=$searchsell;
	
		}
		$this->view->productCat=$searchsell;
	}
	
	public function searchsellAction()  // view helper searchPartical()== 'TIM KIEM THEO PHAN KHUC' tren trinh duyet.
	{
		$id= $this->_request->getParam('getId');  //Lay Id tren link trinh duyet(Link URL nay tao ra khi click vao lien ket va da duoc hien thi tren trinh duyet)
		$product= new Default_Model_Product();
		$searchsell= $product->getSearchSell($id)->toArray();
		
		$this->view->productCat=$searchsell;
	}
	
	public function resultAction() // view helper searchCpu()== 'TIM THEO BO VI XU LY' tren trinh duyet
	{
		//Khac voi Controller Product va Action filter() cua no la loc va tim kiem gia tri trong 1 column cua table trong CSDL.
		//resultAction() va filterAction o duoi trong Controller Search nay la loc va tim kiem tu khoa (Dong CPU )nhung lai la cac doan text nam trong phan mo ta cu the chuc nang ... cua san pham
		
		
		$content=$this->_request->getParam('getInfo');
		
		//Open the index to search in.
		$Index = Zend_Search_Lucene::open('application/searchindex');
		//Set the properties for the Index.
		$analyzer =new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive();
	
		//$analyzer = new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum();
	    Zend_Search_Lucene_Analysis_Analyzer::setDefault($analyzer);
		Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
	    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
	
		//Construct Query
		$query = "\"".$content."\"" ;
	
		$query = Zend_Search_Lucene_Search_QueryParser::parse($query);
		//Search.
		$hits = $Index->find($query);
		
		$this->view->productCat=$hits;

	}
	
	public function filterAction()
	{
		
		$content=$this->_request->getPost();
		
		$Index = Zend_Search_Lucene::open('application/searchindex');
		$analyzer=new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive();
		Zend_Search_Lucene_Analysis_Analyzer::setDefault($analyzer);
		Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
        Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
		
		//Search.
		$a= $content["txtKey"];
		$b=$content['category'];
	
		$query = "product_name:\"".$a."\" And catalory_id:".$b."";
	
		$query = Zend_Search_Lucene_Search_QueryParser::parse($query);
			$hits = $Index->find($query);
		
		$this->view->productCat=$hits;
	}
	
}